package com.application.Tree;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import android.R.color;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.Style;

public abstract class INode {

	protected long id;
	protected String text="";
	protected String title="";
	public static final String SPLIT_TEXT = "#";
	private BoardView boardView;
	protected float x = 0;
	protected float y = 0;
	protected  int heigh = 50;
	protected  int width = 100;
	protected String user;
	int depth=0;
	boolean isZoomIn=true;
	int bgColor=Color.WHITE;
	public abstract void add(INode n);
	public abstract void remove(INode n);	
	public abstract INode getById(long id);
	public abstract ArrayList<INode> getChildren();
	public abstract INode getCollision(float x,float y);
	public abstract void moveAll(float moveX, float moveY);
	public abstract void autoArrange();
	public abstract int getSumChildren();
	public abstract int getNodeHeight();
	public abstract Element toXmlText(Document doc);
	public abstract INode readXML(Element element);
	public abstract boolean clearChildren();
	public abstract void zoomIn();
	public abstract boolean haveOtherChildren(String myUserName);
	
	protected INode(){
		this.text="";
		this.title="";
		this.id=-1;
	}
//	protected INode(long id, String text,float x,float y, String user){
//		this.id=id;
//		this.text=text;
//		this.x=x;
//		this.y=y;
//		this.user=user;
//	}
	protected INode(long id, String title,String text,float x,float y, String user){
		this.id=id;
		this.text=text;
		this.title=title;
		this.x=x;
		this.y=y;
		this.user=user;
	}
	protected void onDraw(Canvas canvas) {
		ArrayList<String> lines = Split(this.title, 10);
//		if (lines.size() >= 3)
//			heigh = 20 + 15 * lines.size();

		RectF rect = new RectF(x, y, x + width, y + heigh);
		int space = 1;
		RectF border = new RectF(x - space, y - space, x + width + space, y
				+ heigh + space);

		Paint p1 = new Paint();
		p1.setAntiAlias(true);
		p1.setStrokeWidth(2);
		// if (this.parentNode == null) {
		// p1.setARGB(255, 138, 148, 84);
		// // p.setARGB(10, 79, 129, 189);
		// canvas.drawRoundRect(rect, 10, 10, p1);
		//
		// Rect bounds = new Rect();
		// p1.setColor(Color.BLACK);
		// canvas.drawText(this.user + ":", x + 5, y + 15, p1);
		// p1.setColor(Color.WHITE);
		// for (int i = 0; i < lines.size(); i++) {
		// p1.getTextBounds(lines.get(i), 0, lines.get(i).length(), bounds);
		// canvas.drawText(lines.get(i), x + 20, y + 30 + i * 15, p1);
		// }
		// if (parentNode != null) {
		// ConnectorLine line = new ConnectorLine(parentNode.getX() +
		// parentNode.width / 2, parentNode.getY()
		// + parentNode.heigh, x + width / 2, y);
		// line.onDraw(canvas);
		// }
		// // p.setColor(Color.rgb(138, 148, 84));
		// }
		// else
		int textColor=1;
		{
			//p1.setARGB(150, 79, 129, 189);
			p1.setColor(this.bgColor);
			canvas.drawRoundRect(rect, 10, 10, p1);

			Rect bounds = new Rect();
			//cai userName nguoi tao node
			if(this.bgColor==Color.BLACK || this.bgColor==Color.BLUE || this.bgColor==Color.RED || this.bgColor==Color.DKGRAY || this.bgColor==Color.MAGENTA)
			{
				p1.setColor(Color.WHITE);
				textColor=p1.getColor();
			}
			else
			{
				p1.setColor(Color.BLACK);
				textColor=p1.getColor();
				
			}
			canvas.drawText(this.user + ":"+this.id, x + 5, y + 15, p1);
			if(this.bgColor==Color.WHITE || this.bgColor==Color.CYAN || this.bgColor==Color.YELLOW || this.bgColor==Color.LTGRAY|| this.bgColor==Color.GREEN || this.bgColor==Color.TRANSPARENT)
				p1.setColor(Color.BLACK);
			else
				p1.setColor(Color.WHITE);
			p1.setTextSize(12);
			String s=lines.get(0);
			if(lines.size()>1)
				s+="...";
			canvas.drawText(s, x + 10, y + 37, p1);
//			for (int i = 0; i < lines.size(); i++) {
//				p1.getTextBounds(lines.get(i), 0, lines.get(i).length(), bounds);
//				canvas.drawText(lines.get(i), x + 20, y + 30 + i * 15, p1);
//			}
			// //Vẽ đường nối
			// if (parentNode != null) {
			// ConnectorLine line = new ConnectorLine(parentNode.getX() +
			// parentNode.width / 2, parentNode.getY()
			// + parentNode.heigh, x + width / 2, y);
			// line.onDraw(canvas);
			// }
		}
		
		//Vẽ đường gạch ngang
		p1.setStrokeWidth(1);
		p1.setColor(textColor);
		
		
		p1.setTextSize(12);
		p1.setTextScaleX(1);
		

		DashPathEffect dash=new DashPathEffect(new float[]{5,5}, (float)1.0);
		p1.setPathEffect(dash);
		p1.setStyle(Style.STROKE);
		canvas.drawLine(this.x, this.y+20, this.x+width, this.y+20, p1);
		
		
		Paint p = new Paint();
		p.setStrokeWidth(2);
		p.setStyle(Paint.Style.STROKE);
		p.setAntiAlias(true);

		p.setColor(Color.rgb(56, 93, 138));
		canvas.drawRoundRect(border, 10, 10, p);
	}

	public ArrayList<String> Split(String text, int len) {
		String[] m = text.split(" ");
		String[] line = new String[m.length];
		if (m.length > 0) {
			int n = m.length;
			int j = 0;
			for (int i = 0; i < n; i++) {
				if (m[i].length() >= len) {
					line[j] = m[i];
					j++;
				} else {
					if (line[j] == null) {
						line[j] = m[i];

					} else {
						if ((line[j] + " " + m[i]).length() > len) {
							line[j + 1] = m[i];
							j++;
						} else {
							line[j] += " " + m[i];
							// j++;
						}

					}
				}
			}
		}
		ArrayList<String> rs = new ArrayList<String>();
		for (int i = 0; i < line.length; i++) {
			if (line[i] != null) {
				rs.add(line[i]);
			}
		}
		return rs;
		// return rs.toArray(java.lang.String);
	}
	
	protected boolean isCollision(float x, float y) {
		return (x > this.x && x < this.x + width) && (y > this.y && y < this.y + heigh);
	}
	public void setText(String newText){
		this.text=newText;
	}
	protected void setPostion(float x, float y){
		this.x=x;
		this.y=y;
	}
	public  String toParse(){
		StringBuffer buffer=new StringBuffer();
		buffer.append(this.id+SPLIT_TEXT);
		buffer.append(this.title+SPLIT_TEXT);
		buffer.append(this.text+SPLIT_TEXT);
		buffer.append(this.x+SPLIT_TEXT);
		buffer.append(this.y+SPLIT_TEXT);
		buffer.append(this.user+SPLIT_TEXT);
		buffer.append(this.depth+SPLIT_TEXT);
		buffer.append(this.bgColor+SPLIT_TEXT);
		
		return buffer.toString();
	}
	public  void parse(String s){
		String []m=s.split(INode.SPLIT_TEXT);
		this.id=Long.parseLong(m[0]);
		this.title=m[1];
		this.text=m[2];
		this.x=Float.parseFloat(m[3]);
		this.y=Float.parseFloat(m[4]);
		this.user=m[5];
		this.depth=Integer.parseInt(m[6]);
		this.bgColor=Integer.parseInt(m[7]);
	}
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public int getHeigh() {
		return heigh;
	}

	public void setHeigh(int heigh) {
		this.heigh = heigh;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getText() {
		return text;
	}

	public int getDepth() {
		return depth;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}
	public int getBgColor() {
		return bgColor;
	}
	public void setBgColor(int bgColor) {
		this.bgColor = bgColor;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	
}
